﻿var lstrRet = 0;
var G_Login=0;
var G_oObject=null;
var G_bMainWindowShowCalled=false;
var G_bMainWindowCreated=0;
var company = "";
var factory = "";
var plant = "";

(function (w) {
    var isIE = function(){
        if(!!window.ActiveXObject || "ActiveXObject" in window)
        {
            return true;
        }
        return false;
    };
    var uaMatch = function (e) {
        e = e.toLowerCase();
        var t = /(chrome)[ \/]([\w.]+)/.exec(e)
            || /(webkit)[ \/]([\w.]+)/.exec(e)
            || /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(e)
            || /(msie) ([\w.]+)/.exec(e)
            || /(trident) ([\w.]+)/.exec(e)
            || e.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(e) || [];
        if(isIE() && t[1] != "msie"){
            t[1] = "msie";
        }
        return {
            browser: t[1] || "",
            version: t[2] || "0"
        }
    };
    var e = uaMatch(w.navigator.userAgent);
    w.checkIE = function (version) {
        var result = e.browser == "msie";
        if (result && version) {
            var v = Math.floor(e.version);
            result = version == v;
        }
        return result;
    };
    w.isLoagModule = function(asModuleName){
        if(typeof asModuleName == "string"){
            if($("a[name='" + asModuleName + "']").length > 0)
            {
                return true;
            }
        }
        return false;
    };
})(window);

//初始化加载
$(function () {

    isIE = checkIE();

    if (!isIE) {
        alert("此模块只能在IE浏览器下使用!");
        return false;
    }

    G_Login = 0;
    G_oObject = null;
    G_bMainWindowShowCalled = false;
    window.setTimeout(checkData, 200); //启动监测系统

});

function getClientPaintSize() {

    var lnHeight = (document.documentElement.clientHeight == 0) ? document.body.clientHeight : document.documentElement.clientHeight;
    var lnWidth = (document.documentElement.clientWidth == 0) ? document.body.clientWidth : document.documentElement.clientWidth;

    lnHeight = (lnHeight - 55);

    return {
        'width': lnWidth,
        'height': lnHeight
    };
}

//初始化加载客户端
function checkData() {

    G_oObject = document.getElementById('DrawingControl');

    if (!sinadot_isValidObject(G_oObject)) {

        var size = getClientPaintSize();
        lnHeight = size.height;
        lnWidth = size.width;
        var obj = document.createElement("object");
        var loContainer = document.getElementById('onlineContent');
        loContainer.appendChild(obj);
        obj.setAttribute("width", "100%");
        obj.setAttribute("height", "" + (lnHeight));
        obj.setAttribute("codeBase", "BHClient.CAB#version=1,0,0,001");
        obj.setAttribute("classid", "CLSID:38C9B0EC-68CD-4C30-AC74-B1A1FE18841A");
        obj.id = "DrawingControl";
        window.setTimeout(checkData, 200);
        return;

    }else{

        var size = getClientPaintSize();
        lnHeight = size.height;
        lnWidth = size.width;
        var obj = document.getElementById("DrawingControl");
        obj.setAttribute("width", "100%");
        obj.setAttribute("height", "" + (lnHeight));
        obj.setAttribute("codeBase", "BHClient.CAB#version=1,0,0,001");
        obj.setAttribute("classid", "CLSID:38C9B0EC-68CD-4C30-AC74-B1A1FE18841A");
    }

    var lstrRet = 0;

    try {

        var obj = document.createElement("DrawingControl");
        obj.setAttribute("width", "100%");
        obj.setAttribute("height", "" + (lnHeight));

        lstrRet = G_oObject.GetParameter("inited");
        G_oObject.SetAppMode(1);

    } catch (e) {
        window.setTimeout(checkData, 200);
    }

    //进行登陆操作
    if (lstrRet == 1){
        if (!G_Login) {
            client_login();
            return;
        }

        //插件的创建登陆完成，进入渲染过程
        window.setTimeout(CheckMainWindowStatus, 100);
    }else {
        window.setTimeout(checkData, 200);
    }
}

//监测系统代码开始
function CheckMainWindowStatus() {

    if (!G_bMainWindowShowCalled) {
        G_bMainWindowShowCalled = true;
        G_oObject.ShowMainWindow();
    }

    if (sinadot_isValidObject(G_oObject)) {

        lstrRet = G_oObject.GetParameter("MainWindowCreated");

        if (lstrRet !=1) {
            window.setTimeout(CheckMainWindowStatus, 200);
            return;

        }else{

            G_bMainWindowCreated = 1;
            G_oObject.ShowSpecPlant(G_type);//设置显示的设备分类
            if (!( company == "" && factory == "" && plant == "" )) {
                G_oObject.SwitchPlant3(company, factory, plant, "机组概貌图", "", "", "", "", "", "", "");
            }else{
                G_oObject.SwitchPlant3("", "", "", "机组概貌图", "", "", "", "", "", "", "");
            }

//            //显示客户端
            try {
                G_oObject.ShowWindowEx(1);
            } catch (e) {
            }

            try {
                lbLoginRet = G_oObject.SetFocus();
            }
            catch (e) {
            }

            $('.global-container').hide();
            $('#client-container').show();
            $('#main-container').hide();

            // unblock();
        }

    }else{
        window.setTimeout(checkData(), 200);
    }
}


/**
 * 客户端登陆
 * 只验证后台配置的第一个在线中间件
 */

function client_login() {
    var host=JSON.parse(localStorage.getItem('controlIP')).nodehost;
    var ip=JSON.parse(localStorage.getItem('controlIP')).nodeport;
    G_oObject.SetParameter("Online_Category","系统,旋转机械专用图谱,应急柴油机振动监测系统,报警统计"); //在线功能组
    G_oObject.SetParameter("ip", host+","+ip+"/TCP");
    G_oObject.SetOnlineInfo(host+","+ip+"/TCP", "mgr", "111", 0);
    G_Login = G_oObject.Login("mgr", "111");       //发送登陆指令

    window.setTimeout(checkData,200);
}

//检测对象是否存在
function sinadot_isValidObject(aObj) {
    if (typeof (aObj) == "undefined" || aObj == null) {
        return false;
    }
    return true;
}
